﻿using LedictCoreAuthorityLib;
using System;
using System.IO;

namespace AuthorityCenter.Models
{
    /// <summary>
    /// 授权处理业务类
    /// </summary>
    public class AuthorityModel
    {
        private static Authority authority = new Authority();
        /// <summary>
        /// 获取硬件码
        /// </summary>
        /// <returns></returns>
        public static string GetHcode()
        {
            return authority.GetHardwareCode();
        }
        /// <summary>
        /// 导入授权
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns></returns>
        public static bool ImportFile(string filePath)
        {
            return authority.ImportFile(filePath);
        }
        /// <summary>
        /// 获取硬件码
        /// </summary>
        /// <returns></returns>
        public static string GetHardwareCode()
        {
            return authority.GetHardwareCode();
        }
        /// <summary>
        /// 获取完整授权信息
        /// </summary>
        /// <returns></returns>
        public static string GetShowRightInfo()
        {
            try
            {
                string txt = File.ReadAllText(authority.GetShowRightInfo());
                return txt;
            }
            catch (Exception)
            {
                return "";
            }      
        }
        /// <summary>
        /// 检测服务授权
        /// </summary>
        /// <returns></returns>
        public static bool CheckServerRight()
        {
            return authority.CheckServerRight();
        }
        /// <summary>
        /// 获取法院授权
        /// </summary>
        /// <param name="courtId">法院ID</param>
        /// <returns></returns>
        public static bool CheckCourtRight(string courtId)
        {
            return authority.CheckCourtRight(courtId);
        }
        /// <summary>
        /// 获取系统授权
        /// </summary>
        /// <param name="courtId">法院ID</param>
        /// <param name="sysCode">系统code</param>
        /// <returns></returns>
        public static bool CheckSystemRight(string courtId, string sysCode)
        {
            return authority.CheckSystemRight(courtId, sysCode);
        }
        /// <summary>
        /// 获取案由授权
        /// </summary>
        /// <param name="courtId">法院ID</param>
        /// <param name="sysCode">系统code</param>
        /// <param name="ccId">案由ID</param>
        /// <returns></returns>
        public static string CheckCauseRight(string courtId, string sysCode, string ccId)
        {
            string str = authority.CheckCauseRight(courtId, sysCode, ccId);
            return str;
        }
        /// <summary>
        /// 获取法院授权到期时间
        /// </summary>
        /// <param name="courtId">法院ID</param>
        /// <param name="sysCode">系统code</param>
        /// <returns></returns>
        public static int GetExpireTime(string courtId,string sysCode)
        {
            return authority.GetExpireTime(courtId, sysCode);
        }
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="srcStr">加密串</param>
        /// <returns></returns>
        public static string Encryp(string srcStr)
        {
            return authority.Encryp(srcStr);
        }
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="srcStr">解密串</param>
        /// <returns></returns>
        public static string Decryp(string srcStr)
        {
            return authority.Decryp(srcStr);
        }
        /// <summary>
        /// 获取法院法庭授权
        /// </summary>
        /// <param name="fyCourtObject">请求参数</param>
        /// <returns></returns>
        public static PiecewiseResults GetFyCourtRightResult(PostFyCourtObject fyCourtObject)
        {
            PiecewiseResults results = new PiecewiseResults();
            string versionVal = "";
            int remainTime = 0;
            try
            {
                if (CheckSystemRight(fyCourtObject.FyCode, fyCourtObject.SystemCode))
                {
                    int systemTime = AuthorityModel.GetExpireTime(fyCourtObject.FyCode, fyCourtObject.SystemCode);
                    if (systemTime > 0)
                    {
                        string version_qx_time = "";
                        if (string.IsNullOrEmpty(fyCourtObject.Hcode))
                        {
                            version_qx_time = authority.CheckFyCourtRightNoHcode(fyCourtObject.FyCode, fyCourtObject.Fy, fyCourtObject.Court);
                        }
                        else
                        {
                            version_qx_time = authority.CheckFyCourtRight(fyCourtObject.FyCode, fyCourtObject.Fy, fyCourtObject.Court, fyCourtObject.Hcode);
                        }
                        var lists = version_qx_time.Split(',');
                        if (lists.Length > 2)
                        {
                            versionVal = lists[0];
                            var currTime = DateTime.Now;
                            var userDays = (currTime - Convert.ToDateTime(lists[2])).Days;
                            remainTime = Convert.ToInt32(lists[1]) - userDays;
                            remainTime = remainTime < systemTime ? remainTime : systemTime;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                results.result.code = "0";
                results.result.message = ex.Message;
            }
            results.body = new { version = versionVal, remaintime = remainTime };
            return results;
        }
    }
    /// <summary>
    /// 上传对象
    /// </summary>
    public class PostObject
    {
        /// <summary>
        /// 上传上来的字符串
        /// </summary>
        [Newtonsoft.Json.JsonProperty(PropertyName = "text")]
        public string srcStr { get; set; }
    }
    /// <summary>
    /// 结果
    /// </summary>
    public class ResultObject
    {
        /// <summary>
        /// 结果内容
        /// </summary>
        public string result { get; set; }
    }

    /// <summary>
    /// /法院法庭授权post
    /// </summary>
    public class PostFyCourtObject
    {
        /// <summary>
        /// 法院编号
        /// </summary>
        public string FyCode { get; set; }
        /// <summary>
        /// 法院
        /// </summary>
        public string Fy { get; set; }
        /// <summary>
        /// 法庭
        /// </summary>
        public string Court { get; set; }
        /// <summary>
        /// 硬件码
        /// </summary>
        public string Hcode { get; set; }
        /// <summary>
        /// 系统编码
        /// </summary>
        public string SystemCode { get; set; }
    }
    /// <summary>
    /// 标准返回类
    /// </summary>
    public class PiecewiseResults
    {
        /// <summary>
        /// 结果
        /// </summary>
        public BaseResult result { get; set; } = new BaseResult();
        /// <summary>
        /// 内容
        /// </summary>
        public object body { get; set; }
    }
    /// <summary>
    /// 基础结果
    /// </summary>
    public class BaseResult
    {
        /// <summary>
        /// 操作结果状态码 1:成功 0:失败
        /// </summary>
        public string code { get; set; } = "1";
        /// <summary>
        /// 状态码对应的信息(一般错误信息)
        /// </summary>
        public string message { get; set; } = "";
    }
}